﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright company="DHGMS Solutions" file="MsSql.cs">
//   2004-2012 DHGMS Solutions. Some Rights Reserved. Licensed under GNU General Public License version 2 (GPLv2)
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Dhgms.TplHelper.Controller.Task.SqlBulkCopy
{
    using System.Data.Common;
    using System.Data.SqlClient;

    using Dhgms.TplHelper.Model.Info;

    /// <summary>
    /// Task for bulk importing from a remote MSSQL Server
    /// </summary>
    public abstract class MsSql<TParameterClass> : Database<TParameterClass>
        where TParameterClass : DbParameter
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the MS SQL bulk copy class. 
        /// </summary>
        /// <param name="taskName">
        /// Name of the data being bulk imported
        /// </param>
        /// <param name="sourceConnectionString">
        /// Connection String for the source database
        /// </param>
        /// <param name="sourceSql">
        /// SQL to get the source data
        /// </param>
        /// <param name="destinationConnectionString">
        /// Connection String for the target database
        /// </param>
        /// <param name="destinationDatabase">
        /// Name of the database the table sits in
        /// </param>
        /// <param name="destinationSchema">
        /// the schema the destination table sits in
        /// </param>
        /// <param name="destinationTable">
        /// the table the data is going to end up in
        /// </param>
        /// <param name="targetTableDeleteAction">
        /// the delete action to be taken on the target table
        /// </param>
        protected MsSql(
            string taskName, 
            string sourceConnectionString, 
            string sourceSql, 
            string destinationConnectionString,
            string destinationDatabase,
            string destinationSchema, 
            string destinationTable, 
            DestinationTableDeleteAction targetTableDeleteAction)
            : base(
                taskName, 
                sourceConnectionString, 
                sourceSql, 
                destinationConnectionString,
                destinationDatabase,
                destinationSchema, 
                destinationTable, 
                targetTableDeleteAction)
        {
        }

        #endregion

        #region Methods

        /// <summary>
        /// Gets the db command for the source database
        /// </summary>
        /// <param name="sourceConnection">
        /// The source Connection.
        /// </param>
        /// <param name="sourceSql">
        /// The source Sql.
        /// </param>
        /// <returns>
        /// db command for the source database
        /// </returns>
        protected override DbCommand GetSourceDbCommand(DbConnection sourceConnection, string sourceSql)
        {
            return new SqlCommand(sourceSql, (SqlConnection)sourceConnection);
        }

        /// <summary>
        /// gets the db connection for the source db
        /// </summary>
        /// <param name="sourceConnectionString">
        /// The source Connection String.
        /// </param>
        /// <returns>
        /// db connection for the source db
        /// </returns>
        protected override DbConnection GetSourceDbConnection(string sourceConnectionString)
        {
            return new SqlConnection(sourceConnectionString);
        }

        #endregion
    }
}